/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	BMenuMgr.java
 * 模块说明：	
 * 修改历史：
 * Apr 30, 2007 - lxm - 创建。
 */
package com.hd123.rumba.navigation.menu;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.domain.biz.IBDomainMgr;
import com.hd123.rumba.domain.biz.SDomain;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.IManager;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.biz.BManager;

/**
 * 菜单 | 业务层 | 管理员 | 实现类
 * 
 * @author lxm
 * 
 */
public class BMenuMgr extends BManager implements IBMenuMgr {

  @Override
  public IManager getPerzMgr() throws BusinessException {
    return null;
  }

  public BMenu createMenu() throws BusinessException, HibernateException {
    SDomain domain = getBDomainMgr().getRootDomain();
    BMenu biz = new BMenu();
    biz.setDomain(domain);
    return biz;
  }

  public BMenu createMenu(String domainUuid) throws BusinessException, HibernateException {
    this.checkParameterNotNull(domainUuid, "domainUuid");
    SDomain domain = getBDomainMgr().getDomain(domainUuid);
    BMenu biz = new BMenu();
    biz.setDomain(domain);
    return biz;
  }

  public BMenu cloneSubtree(BMenu menu, BMenuItem item) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(menu, "menu");
    this.checkParameterNotNull(item, "item");
    assert menu.equals(item.getMenu());

    BMenu tmenu = createMenu(menu.getDomain().getUuid());
    doCloneSubtree(menu, item, tmenu, null);
    return tmenu;
  }

  private IBDomainMgr bdmMgr = null;

  private IBDomainMgr getBDomainMgr() throws BusinessException {
    if (bdmMgr == null)
      bdmMgr = (IBDomainMgr) NamingMgr.getInstance().lookupMgr(IBDomainMgr.class);
    return bdmMgr;
  }

  private void doCloneSubtree(BMenu sm, BMenuItem smiParent, BMenu tm, BMenuItem tmiParent)
      throws BusinessException {
    BMenuItem smi = sm.getFirstChild(smiParent);
    while (smi != null) {
      BMenuItem tmi = new BMenuItem();
      tmi.copyValues(smi);
      tm.addItem(tmi, tmiParent);
      doCloneSubtree(sm, smi, tm, tmi);
      smi = sm.getNextItem(smi);
    }
  }

}
